(function(){
    "use strict";
    app.controller('environmentCtrl', ['$scope', 'httpLoad', '$rootScope','$modal','$state','$timeout','LANGUAGE',
        function($scope, httpLoad, $rootScope, $modal,$state, $timeout,LANGUAGE) {
            $rootScope.moduleTitle = ' 系统管理> 环境';//定义当前页
            // $rootScope.link = '/statics/css/user.css';//引入页面样式
            $scope.param = {
                rows: 10
            };
            $scope.isbatchDelete = true;
            //获取权限列表
            $scope.getData = function(page){
                $scope.param.page = page || $scope.param.page;
                var params = {
                        page: $scope.param.page,
                        rows: $scope.param.rows,
                        envName: $scope.searchByEnvName
                    };
                httpLoad.loadData({
                    url:'/env/list',
                    method: 'POST',
                    data: params,
                    //noParam: true,
                    success:function(data){
                        if(data.rows&&data.rows.length!=0){
                            $scope.envList = data.rows;
                            $scope.totalCount = data.records;
                            $scope.isImageData = false;
                        }else{
                            $scope.isImageData = true;
                        }
                    }
                });
            };
            
            $scope.getData(1);
            //新增
            $scope.add = function(){  //打开模态
                var modalInstance = $modal.open({
                    templateUrl : '/statics/tpl/System/environment/add.html',
                    controller : 'addEnvModalCtrl'// 初始化模态范围
                });
                modalInstance.result.then(function(data){
                    $scope.getData();
                },function(){});
            };
          //删除
            $scope.remove = function(envId){  //打开模态
                var modalInstance = $modal.open({
                    templateUrl : '/statics/tpl/System/environment/delete.html',
                    controller : 'deleteEnvModalCtrl',
                    resolve : {
                        envId: function() {
                            return  envId;
                        }
                    }
                });
                modalInstance.result.then(function(){
                    $scope.getData();
                    $scope.isCheck = false;
                },function(){});
            };
          //指定环境管理员
            $scope.designateUser = function(id){  //打开模态
                var modalInstance = $modal.open({
                    templateUrl : '/statics/tpl/System/environment/clusterList.html',  //集群信息
                    controller : 'clusterEnvModalCtrl',// 初始化模态范围
                    resolve : {
                    	id: function() {
                            return  id;
                        }
                    }
                });
                modalInstance.result.then(function(data){
//                    angular.extend(item,data);
                },function(){});
            };
            
            //编辑
            $scope.update = function(item){  //打开模态
                var modalInstance = $modal.open({
                    templateUrl : '/statics/tpl/System/environment/update.html',  //指向上面创建的视图
                    controller : 'clusterEnvUpdateModalCtrl',// 初始化模态范围
                    resolve : {
                        itemData: function() {
                            return item;
                        }
                    }
                });
                modalInstance.result.then(function(data){
                    angular.extend(item,data);
                },function(){});
            };
        }
    ]);
    //新增ctrl
    angular.module('app').controller('addEnvModalCtrl',['$scope','$modalInstance','LANGUAGE','httpLoad',
        function($scope,$modalInstance,LANGUAGE,httpLoad){
    	
    	httpLoad.loadData({
	          url:'/runtime/cluster/listAll',
	          //data: {envId:id},
	          //noParam: true,
	          success:function(data){
	              if(data.data&&data.data.length!=0){
	            	 $scope.clustList = data.data
	              }else{
	            	  $scope.pop(data.message,'error');
	                  $scope.isImageDataCluster = true;
	              }
	          }
	      });
    	  
    	  $scope.cluster ={
    			  
    		}
            $scope.ok = function(){
    		  $scope.params = {
    	            	envName: $scope.addData.envName,
    	            	envDesc:$scope.addData.envDesc,
    	            	clusterId:$scope.addData.cluster.clusterId,
    	            	clusterServiceIp:$scope.addData.cluster.serviceIp,
    	        	};
                httpLoad.loadData({
                    url:'/env/createEnv',
                    method:'POST',
                    data: $scope.params,
                    noParam:true,
                    success:function(data){
                        if(data.success){
                            $scope.pop(data.message);
                            $modalInstance.close($scope.addData);
                        }
                    }
                });
            };
            $scope.cancel = function(){
                $modalInstance.dismiss('cancel'); // 退出
            };
        }]);
    //删除ctrl
    angular.module('app').controller('deleteEnvModalCtrl',['$scope','$modalInstance','envId','LANGUAGE','httpLoad',
        function($scope,$modalInstance,envId,LANGUAGE,httpLoad){ //依赖于modalInstance
            $scope.ok = function(){
                httpLoad.loadData({
                    url:'/env/delete',
                    method:'POST',
                    data:{envId: envId},
                    noParam:true,
                    success:function(data){
                        if(data.success){
                            $scope.pop(data.message);
                            $modalInstance.close();
                        }else{
                            $scope.pop(data.message);
                            $modalInstance.close();
                        }
                    }
                });
            };
            $scope.cancel = function(){
                $modalInstance.dismiss('cancel'); // 退出
            }
        }]);
  //环境指定环境管理员
    angular.module('app').controller('clusterEnvModalCtrl',['$scope','$modalInstance','LANGUAGE','httpLoad','id',
        function($scope,$modalInstance,LANGUAGE,httpLoad,id){ //依赖于modalInstance
    	$scope.getData = function(page){
    		 $scope.param = {
    	                rows: 5
    	            };
      	  $scope.param.page = page || $scope.param.page;
            var params = {
                    page: $scope.param.page,
                    rows: $scope.param.rows,
                    envId:id
                }
    	httpLoad.loadData({
		          url:'/user/list',
		          method:'POST',
                  data:params,
		          success:function(data){
		        	  if(data.rows&&data.rows.length!=0){
                          $scope.userList = data.rows;
                          $scope.totalCount = data.records;
                          $scope.isImageData = false;
                          angular.forEach($scope.userList, function(data,index){
                              if(data.isLocked){
                                  data.isfreeze=true;
                                  data.isthaw=false;
                              }else{
                                  data.isfreeze=false;
                                  data.isthaw=true;
                              }
                          });
                      }else{
                          $scope.isImageData = true;
                      }
		          }
		      });
    	};
    		$scope.getData(1);
    		
    		$scope.cid = {
    		};
            $scope.ok = function(){
            	var envId = id;
        		var userId = $scope.cid.userId;
                httpLoad.loadData({
                    url:'/role/addEnvManager',
                    method:'POST',
                    data:JSON.stringify({envId:envId,userId:userId}),
                    success:function(data){
                        if(data.success){
                            $scope.pop(data.message);
                            $modalInstance.close();
                        }else{
                            $scope.pop(data.message);
                            $modalInstance.close();
                        }
                    }
                });
            };
            $scope.cancel = function(){
                $modalInstance.dismiss('cancel'); // 退出
            }
        }]);
  //编辑ctrl
    angular.module('app').controller('clusterEnvUpdateModalCtrl',['$scope','$modalInstance','itemData','LANGUAGE','httpLoad',
        function($scope,$modalInstance,itemData,LANGUAGE,httpLoad){
            $scope.updateData = {};
            $scope.updateData.envId = itemData.envId;
            $scope.updateData.envName = itemData.envName;
            $scope.updateData.envDesc = itemData.envDesc;
            $scope.ok = function(){
                httpLoad.loadData({
                    url:'/env/updateEnv',
                    method:'POST',
                    data: $scope.updateData,
                    noParam:true,
                    success:function(data){
                        if(data.success){
                            $scope.pop(data.message);
                            $modalInstance.close($scope.updateData);
                        }
                    }
                });
            };
            $scope.cancel = function(){
                $modalInstance.dismiss('cancel'); // 退出
            };
        }]);
})();